<template>
	<view>
		<view class='header'>
			<image src='../../static/images/wx.jpg'></image>
		</view>
		<view class='content'>
			<view>申请获取以下权限</view>
			<text>获得你的公开信息(昵称，头像、地区等)</text>
		</view>
		<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="getuserinfo">
			授权登录
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: null,
				encryptedData: null,
				iv: null,
				newCode: null,
				newEncryptedData: null,
				newIv: null
			};
		},
		onLoad(){

		},
		methods:{
			getuserinfo (res){
				console.log(res,'rrrrrrrrrrrrrrrrrrrr')
				let self = this;
				self.encryptedData = res.detail.encryptedData;
				self.iv = res.detail.iv;
				uni.login({
					provider: 'weixin',
					success: function (codes) {
						self.code = codes.code;
						self.testLogin();
					},
					fail(e){
						console.log(e);
					}
				});
            },
			//获取新的code iv enencryptedData
			newInfo(){
				let self = this;
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						self.newCode = loginRes.code;
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							success: function (infoRes) {
								self.newIv = infoRes.iv;
								self.newEncryptedData = infoRes.encryptedData;
								self.register();
							}
						});
					}
				});
			},
			//验证登录
			testLogin(){
				let self = this;
				uni.request({
					url: self.$store.state.URL+"login/login/mini_login",
					data: {
						code: self.code
					},
					success(res) {
						console.log(res);
						if(res.data.flag == 1){
							console.log("登录成功");
							self.$store.commit('updataToken',res.data.token);
							uni.switchTab({
								url: '../tabBar/home/home'
							});
							console.log(res.data.token, self.$store.state.token, '用户token')
						}else if(res.data.flag == 2){
							console.log("登陆成功但没有token");
						}else if(res.data.flag == 0){
							console.log("用户未注册");
							self.newInfo();
						}else{
							console.log("系统错误");
						}
					},
					fail(e){
						console.log(e);
					}
				});
			},
			//请求注册
			register(){
				let self = this;
				uni.request({
					url: self.$store.state.URL+'login/login/mini_register',
					data: {
						code: self.newCode,
						encryptedData: self.newEncryptedData,
						iv: self.newIv,
					},
					success(msg) {
						if(msg.data.flag == 1){
							console.log("注册成功");
							uni.switchTab({
								url: '../tabBar/home/home'
							});
						}else if(msg.data.flag == 2){
							console.log("已注册");
							uni.switchTab({
								url: '../tabBar/home/home'
							});
						}else if(msg.data.flag == 0){
							console.log("授权失败");
						}else{
							console.log("系统错误");
						}
					},
					fail(e) {
						console.log(e);
					}
				});
			}
		},
	}
</script>

<style scoped>
	 .header {
        margin: 90rpx 0 90rpx 50rpx;
        border-bottom: 1px solid #ccc;
        text-align: center;
        width: 650rpx;
        height: 300rpx;
        line-height: 450rpx;
    }

    .header image {
        width: 200rpx;
        height: 200rpx;
    }

    .content {
        margin-left: 50rpx;
        margin-bottom: 90rpx;
    }

    .content text {
        display: block;
        color: #9d9d9d;
        margin-top: 40rpx;
    }

    .bottom {
        border-radius: 80rpx;
        margin: 70rpx 50rpx;
        font-size: 35rpx;
    }
</style>